//
//  SubCollectionCellOne.m
//  
//
//  Created by dllo on 16/1/26.
//
//

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "SubCollectionCellOne.h"
#import "UIImageView+WebCache.h"
#import "ModelForCollectionViewTwo.h"

#import "NetworkHandlerBlock.h"
#import "ModelForTypeOfDetail.h"
#import "ModelForTypeOfListOfDetail.h"
#import "DetailOfBookViewController.h"

#import "UIButton+WebCache.h"
#import "ModelForListen.h"



@interface SubCollectionCellOne ()

@property (nonatomic, retain) UIView *view;
@property (nonatomic, retain) UIButton *buttonBook;
@property (nonatomic, retain) UIButton *buttonMusic;
@property (nonatomic, retain) UIButton *buttonYuLe;
@property (nonatomic, retain) UIButton *buttonXiangSheng;
@property (nonatomic, retain) UIButton *buttonErTong;
@property (nonatomic, retain) NSMutableArray *arrTemp;


@property (nonatomic, retain) NSMutableArray *arrForBook;
@property (nonatomic, retain) NSMutableArray *arrForMusic;
@property (nonatomic, retain) NSMutableArray *arrForYuLe;
@property (nonatomic, retain) NSMutableArray *arrForXiangSheng;
@property (nonatomic, retain) NSMutableArray *arrForErTong;


@end

@implementation SubCollectionCellOne

- (void)dealloc {

    [_view release];
    [_buttonBook release];
    [_buttonMusic release];
    [_buttonYuLe release];
    [_buttonXiangSheng release];
    [_buttonErTong release];
    [_arrTemp release];
    
    [_arrForBook release];
    [_arrForMusic release];
    [_arrForYuLe release];
    [_arrForXiangSheng release];
    [_arrForErTong release];
    [super dealloc];

}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.arrForBook       = [NSMutableArray array];
        self.arrForMusic      = [NSMutableArray array];
        self.arrForYuLe       = [NSMutableArray array];
        self.arrForXiangSheng = [NSMutableArray array];
        self.arrForErTong     = [NSMutableArray array];

        [self createSubViews];
        [self handleData];
    }
    return self;
}


- (void)handleData {
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=3&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
    
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
            ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
            [modelOfBook setValuesForKeysWithDictionary:dic];
            
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ///////////////////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
            [self.arrForBook addObject:modelOfBook];  
            }
        }
    }];
    
    
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=2&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    //////////////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForMusic addObject:modelOfBook];
            }
        }
    }];
    
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=4&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    
                    /////////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForYuLe addObject:modelOfBook];
            }
        }
    }];
    
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=14&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    /////////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForXiangSheng addObject:modelOfBook];
            }
        }
    }];
    
    
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=6&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ////////////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForErTong addObject:modelOfBook];
            }
        }
    }];
    
    
    
}

- (void)createSubViews {

    self.view = [[UIView alloc]initWithFrame:self.bounds];
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.view];

    self.buttonBook = [UIButton buttonWithType:UIButtonTypeCustom];

    [self.buttonBook setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
    self.buttonBook.frame = CGRectMake(10, 10, (WIDTH - 30)/ 2, (WIDTH - 30)/ 2 + 5);
    [self.view addSubview:self.buttonBook];
    [self.buttonBook addTarget:self action:@selector(enterToDetailOfBookViewController:) forControlEvents:UIControlEventTouchUpInside];
    

    self.buttonMusic = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonMusic setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
    self.buttonMusic.frame = CGRectMake(15 + (WIDTH - 30)/ 2, 10, (WIDTH - 30)/ 4, (WIDTH - 30)/ 4);
    [self.view addSubview:self.buttonMusic];
    [self.buttonMusic addTarget:self action:@selector(enterToDetailOfBookViewController1:) forControlEvents:UIControlEventTouchUpInside];
    
    self.buttonYuLe = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonYuLe setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
    self.buttonYuLe.frame = CGRectMake(20 + (WIDTH - 30)*3/4, 10, (WIDTH - 30)/ 4, (WIDTH - 30)/ 4);
    [self.view addSubview:self.buttonYuLe];
    [self.buttonYuLe addTarget:self action:@selector(enterToDetailOfBookViewController2:) forControlEvents:UIControlEventTouchUpInside];
    
    

    self.buttonXiangSheng = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonXiangSheng setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
    self.buttonXiangSheng.frame = CGRectMake(15 + (WIDTH - 30)/ 2, 15 + (WIDTH - 30)/ 4, (WIDTH - 30)/ 4, (WIDTH - 30)/ 4);
    [self.view addSubview:self.buttonXiangSheng];
     [self.buttonXiangSheng addTarget:self action:@selector(enterToDetailOfBookViewController3:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.buttonErTong = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonErTong setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
    self.buttonErTong.frame = CGRectMake(20 + (WIDTH - 30)*3/4, 15 + (WIDTH - 30)/ 4, (WIDTH - 30)/ 4, (WIDTH - 30)/ 4);
    [self.view addSubview:self.buttonErTong];
    // setImage 为来源图片大小. setBackGroundImage 为button大小.
    [self.buttonErTong addTarget:self action:@selector(enterToDetailOfBookViewController4:) forControlEvents:UIControlEventTouchUpInside];
    [self.view release];
}

- (void)enterToDetailOfBookViewController:(UIButton *)button {

    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForBook;
    bookVC.title = @"有声书";
    
    [vc.navigationController pushViewController:bookVC animated:NO];
}

- (void)enterToDetailOfBookViewController1:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForMusic;
    bookVC.title = @"音乐";
    
    [vc.navigationController pushViewController:bookVC animated:NO];
}

- (void)enterToDetailOfBookViewController2:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForYuLe;
    bookVC.title = @"综艺娱乐";
    
    [vc.navigationController pushViewController:bookVC animated:NO];
}


- (void)enterToDetailOfBookViewController3:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForXiangSheng;
    bookVC.title = @"相声";
    
    [vc.navigationController pushViewController:bookVC animated:NO];
}

- (void)enterToDetailOfBookViewController4:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForErTong;
    bookVC.title = @"儿童";
    
    [vc.navigationController pushViewController:bookVC animated:NO];
}

- (void)passModelForCellOne:(NSArray *)arrForModel {

    if (arrForModel) {
        
    
    
    
    self.arrTemp = [NSMutableArray array];

        
    for (ModelForCollectionViewTwo *model in arrForModel) {
        [self.arrTemp addObject:model.coverPath];
    
    }
    // 注:此处为异步加载.图片不会自动赋值到Button上去.因此最开始会赋不上值.
//    UIImageView *pic = [[UIImageView alloc]init];
//    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTemp[0]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//         [self.buttonBook setBackgroundImage:pic.image forState:UIControlStateNormal];
//    }];
    // 此处还可以调用 UIButton+WebCache.h 第三方
    [self.buttonBook sd_setBackgroundImageWithURL:[NSURL URLWithString:self.arrTemp[0]] forState:UIControlStateNormal];
   

    UIImageView *pic1 = [[UIImageView alloc]init];
    [pic1 sd_setImageWithURL:[NSURL URLWithString:self.arrTemp [1]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        [self.buttonMusic setBackgroundImage:pic1.image forState:UIControlStateNormal];
    }];

    
    
    UIImageView *pic2 = [[UIImageView alloc]init];
    [pic2 sd_setImageWithURL:[NSURL URLWithString:self.arrTemp[2]]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    [self.buttonYuLe setBackgroundImage:pic2.image forState:UIControlStateNormal];
    }];

    
    UIImageView *pic3 = [[UIImageView alloc]init];
    [pic3 sd_setImageWithURL:[NSURL URLWithString:self.arrTemp[3]]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    [self.buttonXiangSheng setBackgroundImage:pic3.image forState:UIControlStateNormal];
    }];
    

    
    UIImageView *pic4 = [[UIImageView alloc]init];
    [pic4 sd_setImageWithURL:[NSURL URLWithString:self.arrTemp[4]]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [self.buttonErTong setBackgroundImage:pic4.image forState:UIControlStateNormal];
    }];
}
}

@end
